﻿using System;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Helpers;
using Pharmacy.Windows.Forms.Main;

namespace Pharmacy.Windows.Forms
{
    public partial class MiniSkin : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private MiniFormUsers formUsers = null;
        private MiniFormPrescription formPrescription = null;
        private MiniFormAreaSettings formAreaSettings = null;
        private FormVoid formVoid;
        private FormHistory formHistory;
        private FormStock formStock;
        private Loading sp;
        public MiniSkin()
        {
            InitializeComponent();
            Thread th = new Thread(new ThreadStart(DoSplash));
            th.ApartmentState = ApartmentState.STA;
            th.IsBackground = false;
            th.Start();
            Thread.Sleep(7000);
            th.Abort();
            th.Join();
            //th=new Thread(CloseSphash);
            //th.Start();
            //Thread.Sleep(1000);
            LoadByRoles();
            SkinHelper.InitSkinGallery(rddAppearance, true);
            this.Text = "Pharmacy version " + this.GetType().Assembly.GetName().Version.ToString();
        }


        private void DoSplash()
        {
            sp = new Loading();
            sp.TopMost = true;
            sp.ShowDialog();
            //sp.BeginInvoke(new MethodInvoker(CloseSphash));
        }

        private void CloseSphash()
        {
            Thread.Sleep(7000);
            sp.Close();
        }

        private void LoadByRoles()
        {
            //bPrescription.Enabled = ServiceFacade.SettingsHelper.AllowPrescription;
            formPrescription = new MiniFormPrescription { MdiParent = this };
            formPrescription.Show();

            string roles = Program.GlobalUser.Roles;
            switch (roles)
            {
                case "Pharmacist":
                    formPrescription = new MiniFormPrescription { MdiParent = this };
                    formPrescription.Show();
                    formPrescription.BringToFront();
                    break;
                case "Account":
                    break;
                case "Super-Cashier":
                    break;
                case "Admin":
                    pSettings.Visible = true;
                    break;
            }
            //Test Print Label
            //FormPrescriptionLabel formPrescriptionLabel = new FormPrescriptionLabel();
            //formPrescriptionLabel.LoadDefault("DN-HCM-01-111101004");
            //formPrescriptionLabel.Show();
        }

        private void Users_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (formUsers == null || formUsers.IsDisposed)
            {
                formUsers = new MiniFormUsers { MdiParent = this };
                formUsers.Show();
            }
            else
            {
                formUsers.BringToFront();
            }
        }

        private void Prescription_ItemClick(object sender, ItemClickEventArgs e)
        {
            var formPre = new MiniFormPrescription() { MdiParent = this };
            formPre.Show();
        }

        private void bAreaSettings_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (formAreaSettings == null || formAreaSettings.IsDisposed)
            {
                formAreaSettings = new MiniFormAreaSettings { MdiParent = this };
                formAreaSettings.Show();
            }
            else
            {
                formAreaSettings.BringToFront();
            }
        }

        private void bVoid_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (formVoid == null || formVoid.IsDisposed)
            {
                formVoid = new FormVoid { MdiParent = this };
                formVoid.Show();
            }
            else
            {
                formVoid.BringToFront();
            }
        }

        private void bHistory_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (formHistory == null || formHistory.IsDisposed)
            {
                formHistory = new FormHistory() { MdiParent = this };
                formHistory.Show();
            }
            else
            {
                formHistory.BringToFront();
            }
        }

        private void bStock_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (formStock == null || formStock.IsDisposed)
            {
                formStock = new FormStock() { MdiParent = this };
                formStock.Show();
            }
            else
            {
                formStock.BringToFront();
            }
        }
    }
}